<?php
/*
 * 新锐手机注册 [ 深圳市新锐创意科技有限公司旗下产品 ]
 * Copyright  2015-2099 www.xinruiweb.com  All Rights Reserved.
 * 技术开发: 75674952@qq.com
 * 请支持正版授权产品
 */
//error_reporting(E_ALL);
if( ! defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$auth = array('regcode','register','sendcode','login','logging','getmobile','uptpwd','mlogin','mreg','mupt','msendcode','bind','unbind','bindm','uptuser','uptuserm');

$op = $_REQUEST['op'];
if(!in_array($op, $auth)) {
	showmessage($_G['XRLANG_MOBLIEREG']['wechat_op_failed'], $referer);
}

if (submitcheck('confirmsubmit')) {
    if($op == 'regcode'){
        //error_reporting(E_ALL);
        xinrui_mreg::xr_auth();
        if(!check_seccode($_GET['seccodeverify'], $_GET['seccodehash'])) {
            showmessage('submit_seccode_invalid');
        }
        
        $code_length = !empty($_G['cache']['plugin']['xinrui_mobilereg']['code_length'])?intval($_G['cache']['plugin']['xinrui_mobilereg']['code_length']):6;
        $code = '';
        for($i=0;$i<$code_length;$i++){
            $code  .= rand(1,9); 
        }
        
        $phone = $_POST['phone'];
        if(!preg_match("/^1[34578]\d{9}$/",$phone)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile12']);
        }

        $user = C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->fetch_first_by_moblie($phone); 
        if($user){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile22']);
        }

        
        $id = C::t('#xinrui_mobilereg#xinrui_mobilereg_outhcode')->insert(array(
            'mobile' => $phone,
            'code' => $code ,
            'createtime' => time(),
            'expiredtime' => time() + $_G['cache']['plugin']['xinrui_mobilereg']['code_time'],
        ));

        if(empty($id)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile14']);
        }
        if($_REQUEST['handlekey'] == 'upt'){
            $tmpid = $_G['cache']['plugin']['xinrui_mobilereg']['tmp_id1'];
        }else{
            $tmpid = $_G['cache']['plugin']['xinrui_mobilereg']['tmp_id'];
        }

        if(empty($_G['cache']['plugin']['xinrui_mobilereg']['aliyun']) || !isset($_G['cache']['plugin']['xinrui_mobilereg']['aliyun'])){
          //老版阿里云 发送接口
            spl_autoload_unregister(array('core', 'autoload'));
            include DISCUZ_ROOT.'./source/plugin/xinrui_mobilereg/TopSdk.php';
            $c = new TopClient;
            $c->appkey = $_G['cache']['plugin']['xinrui_mobilereg']['AppKey'];
            $c->secretKey = $_G['cache']['plugin']['xinrui_mobilereg']['AppSecret'];
            $sign = $_G['cache']['plugin']['xinrui_mobilereg']['sign'];
            
            if(CHARSET == 'gbk') {
                    $sign = diconv($sign, mb_detect_encoding($sign, array("ASCII","UTF-8","GB2312","GBK","BIG5")),'UTF-8');
            }
            $req = new AlibabaAliqinFcSmsNumSendRequest;
            $req->setSmsType("normal");
            $req->setSmsFreeSignName($sign);
            $req->setSmsParam("{\"code\":\"".$code."\"}");
            $req->setRecNum($phone);
            $req->setSmsTemplateCode($tmpid);
            $resp = $c->execute($req);
            spl_autoload_register(array('core', 'autoload'));
            if(isset($resp->result->err_code) && $resp->result->err_code == 0){
                showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile13'],'',array(),array('showdialog'=>1,'closetime'=>true));
            }else{
                showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile15'],'',array(),array('showdialog'=>1,'closetime'=>true));
            }
        }else{
            /*以下是新版*/
            header('Content-Type: text/plain; charset=utf-8');
            include_once DISCUZ_ROOT.'./source/plugin/xinrui_mobilereg/module/SmsDemo.php';
            $demo = new SmsDemo(
                $_G['cache']['plugin']['xinrui_mobilereg']['AppKey'],
                $_G['cache']['plugin']['xinrui_mobilereg']['AppSecret']
            );
            $sign = $_G['cache']['plugin']['xinrui_mobilereg']['sign'];
            
            if(CHARSET == 'gbk') {
                $sign = diconv($sign, mb_detect_encoding($sign, array("ASCII","UTF-8","GB2312","GBK","BIG5")),'UTF-8');
            }

            $response = $demo->sendSms(
                $sign, // 短信签名
                $tmpid, // 短信模板编号
                $phone, // 短信接收者
                array("code"=>$code)
            );
            if(isset($response->Code) && $response->Code == 'OK'){
                showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile13'],'',array(),array('showdialog'=>1,'closetime'=>true));
            }else{
                showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile15'],'',array(),array('showdialog'=>1,'closetime'=>true));
            }
        }

    }else if($op == 'register'){
        $phone = $_POST['phone'];
        $verfiy = $_POST['verfiy'];
        $passwd = $_POST['passwd'];
        $passwd_2 = $_POST['passwd_2'];

        if(!preg_match("/^1[34578]\d{9}$/",$phone)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile12']);
        }

        if(empty($passwd)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile20']);
        }
        if($passwd != $passwd_2){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile17']);
        }

        $code = C::t('#xinrui_mobilereg#xinrui_mobilereg_outhcode')->fetch_by_code_status($verfiy,$phone);
        if(empty($code)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile18']);
        }

        $user = C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->fetch_first_by_moblie($phone); 
        if($user){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile22']);
        }

        if(isset($_G['cache']['plugin']['xinrui_mobilereg']['reg_area']) && !empty($_G['cache']['plugin']['xinrui_mobilereg']['reg_area'])){
            if(strpos($_G['cache']['plugin']['xinrui_mobilereg']['reg_area'],',') != false){
                $reg_area = explode(',',$_G['cache']['plugin']['xinrui_mobilereg']['reg_area']);
                $ok = false ;
                foreach($reg_area as $val){
                    if(preg_match("/^".$val."/",$phone)){
                        $ok = true ;
                        break ;
                    }
                }
                if($ok == false ){
                    showmessage($_G['XRLANG_MOBLIEREG']['reg_area_error']);
                }
            }else{
                if(!preg_match("/^".$_G['cache']['plugin']['xinrui_mobilereg']['reg_area']."/",$phone)){
                    showmessage($_G['XRLANG_MOBLIEREG']['reg_area_error']);
                }
            }
        }


    
        $username = 'S'.time();
        $uid = xinrui_mreg::register($username,$passwd);
        if(!$uid){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile21']);
        }


        C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->insert(array(
                'uid' => $uid,
                'username' => $phone,
                'user'=>$username,
                'createtime'=> time(),
                'mobile'=>$phone,
                'bind' => $uid,
            ));

        C::t('#xinrui_mobilereg#xinrui_mobilereg_outhcode')->update($code['id'], array('status' => 1));
        C::t('common_member_profile')->update($uid, array('mobile' => $phone));
        
        $param = array('bbname' => $_G['member']['username'], 'usergroup' => $_G['group']['grouptitle']);
        showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile19'],'forum.php',array(),array('showdialog'=>1,'closetime'=>true));
    }else if($op == 'bindm'){
        $phone = $_POST['phone'];
        $verfiy = $_POST['verfiy'];
        $passwd = $_POST['passwd'];
        $passwd_2 = $_POST['passwd_2'];

        if(!preg_match("/^1[34578]\d{9}$/",$phone)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile12']);
        }

        if(empty($passwd)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile20']);
        }
        if($passwd != $passwd_2){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile17']);
        }

        if(isset($_G['cache']['plugin']['xinrui_mobilereg']['reg_area']) && !empty($_G['cache']['plugin']['xinrui_mobilereg']['reg_area'])){
            if(strpos($_G['cache']['plugin']['xinrui_mobilereg']['reg_area'],',') != false){
                $reg_area = explode(',',$_G['cache']['plugin']['xinrui_mobilereg']['reg_area']);
                $ok = false ;
                foreach($reg_area as $val){
                    if(preg_match("/^".$val."/",$phone)){
                        $ok = true ;
                        break ;
                    }
                }
                if($ok == false ){
                    showmessage($_G['XRLANG_MOBLIEREG']['reg_area_error']);
                }
            }else{
                if(!preg_match("/^".$_G['cache']['plugin']['xinrui_mobilereg']['reg_area']."/",$phone)){
                    showmessage($_G['XRLANG_MOBLIEREG']['reg_area_error']);
                }
            }
        }


        $code = C::t('#xinrui_mobilereg#xinrui_mobilereg_outhcode')->fetch_by_code_status($verfiy,$phone);
        if(empty($code)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile18']);
        }

        loaducenter();

        $user = C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->fetch_first_by_moblie($phone); 
        if(!empty($user)){
            if(!empty($user['user'])){
                $phone = $user['user'];
            }
            $info = uc_user_login($phone,$passwd);
            if($info[0] > 0){
                C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->update($user['id'],array('uid'=>$_G['uid']));
                C::t('#xinrui_mobilereg#xinrui_mobilereg_outhcode')->update($code['id'], array('status' => 1));
                $param = array('bbname' => $_G['member']['username'], 'usergroup' => $_G['group']['grouptitle']);
                showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile23'],'forum.php',array(),array('showdialog'=>1,'closetime'=>true));
            }else{
                showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile25']);
            }
        }


        $olduid = $_G['uid'] ;
        $username = 'S'.time();
        $uid = xinrui_mreg::register($username,$passwd);
        if(!$uid){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile21']);
        }

        C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->insert(array(
                'uid' => $olduid,
                'user'=> $username,
                'username' => $phone,
                'createtime'=> time(),
                'mobile'=>$phone,
                'bind'=>$uid,
            ));

        require_once libfile('function/member');
        $result = getuserbyuid($olduid, 1);
        if(!empty($result)){
            setloginstatus($result, 1296000);
        }
        C::t('#xinrui_mobilereg#xinrui_mobilereg_outhcode')->update($code['id'], array('status' => 1));
        $param = array('bbname' => $_G['member']['username'], 'usergroup' => $_G['group']['grouptitle']);
        showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile23'],'forum.php',array(),array('showdialog'=>1,'closetime'=>true));
    }
    else if($op == 'login'){
        $phone = $_POST['phone'];
        $pwd = $_POST['pwd'];
        $referer = !empty($_POST['referer'])?$_POST['referer']:dreferer();

        if(!preg_match("/^1[34578]\d{9}$/",$phone)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile12']);
        }
        $rs = C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->fetch_first_by_moblie($phone); 
        if(!empty($rs['user'])){
            $phone = $rs['user'];
        }
        loaducenter();
        $info = uc_user_login($phone,$pwd);
        if($info[0] < 0 || empty($info)){
            showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile4']);
        }

        $user = C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->fetch_first_by_bind($info[0]); 
        if(!empty($user['uid'])){
            $info[0] = $user['uid'] ; 
        }

        require_once libfile('function/member');
        $result = getuserbyuid($info[0], 1);
        if(!empty($result)){
            setloginstatus($result, 1296000);
            $ucsynlogin = '';
            if($_G['setting']['allowsynlogin']) {
                $ucsynlogin = uc_user_synlogin($info[0]);
            }
            showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile5'],$referer,array(),array('showdialog'=>1,'closetime'=>true,'extrajs' => $ucsynlogin));
        }
    }
    else if($op == 'getmobile'){    //找回密码 第一步
        session_start();
        $phone = $_POST['phone'];
        $verfiy = $_POST['verfiy'];

        if(!preg_match("/^1[34578]\d{9}$/",$phone)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile12']);
        }

        $code = C::t('#xinrui_mobilereg#xinrui_mobilereg_outhcode')->fetch_by_code_status($verfiy,$phone);
        if(empty($code)){
            //showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile18']);
        }
        $_SESSION['xrmobilereg_phone'] = $phone;
        showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile12'],'forum.php',array(),array('showdialog'=>1,'closetime'=>true));
    }
    else if($op == 'uptpwd'){    //找回密码 第二步
        session_start();
        if(!isset($_SESSION['xrmobilereg_phone']) || empty($_SESSION['xrmobilereg_phone'])){
            showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile13']);
        }

        $passwd = $_POST['password'];
        $passwd_2 = $_POST['password2'];

        if(!preg_match("/^1[34578]\d{9}$/",$_SESSION['xrmobilereg_phone'])){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile12']);
        }

        if(empty($passwd)){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile20']);
        }
        if($passwd != $passwd_2){
            showmessage($_G['XRLANG_MOBLIEREG']['reg_pc_mobile17']);
        }

        $rs = C::t('#xinrui_mobilereg#xinrui_mobilereg_user')->fetch_first_by_moblie($_SESSION['xrmobilereg_phone']); 
        if(!empty($rs['user'])){
            $_SESSION['xrmobilereg_phone'] = $rs['user'];
        }

        if($result = xinrui_mreg::uptpwd($_SESSION['xrmobilereg_phone'],$passwd)){
            showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile14'],'member.php?mod=logging&action=login',array(),array('showdialog'=>1,'closetime'=>true));
        }else{
            showmessage($_G['XRLANG_MOBLIEREG']['login_pc_mobile15']);
        }
    }else if($op == 'bind'){
        if(xinrui_mreg::bindoldusername()){
            showmessage($_G['XRLANG_MOBLIEREG']['setting_bind_username_success'],'',array(),array('showdialog'=>1,'closetime'=>true));
        }else{
            showmessage($_G['XRLANG_MOBLIEREG']['setting_bind_failed3']);
        }
    }else if($op == 'uptuserm'){
        if($_GET['newusername'] != $_G['member']['username']) {
            $username = xinrui_mreg::checkusername($_GET['newusername']);
            if($username){
                if(xinrui_mreg::uptuser($username)){
                    $data['uid'] = $_G['uid'];
                    showmessage($_G['XRLANG_MOBLIEREG']['setting_upt_username_success'],'',array(),array('showdialog'=>1));
                }else{
                    showmessage($_G['XRLANG_MOBLIEREG']['setting_upt_username_failed2']);
                }
            }else{
                showmessage($_G['XRLANG_MOBLIEREG']['setting_upt_username_failed1']);
            }
        }else{
            showmessage($_G['XRLANG_MOBLIEREG']['setting_upt_username_failed']);
        }

    }else if($op == 'unbind'){
        xinrui_mreg::unbind();
    }

}

if($op == 'msendcode'){
    include_once template('xinrui_mobilereg:seccode');
}else{
    include_once template('xinrui_mobilereg:setting');
}




